package com.itheima.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.user.dao.ItemsFootprintMapper;
import com.itheima.user.pojo.ItemsFootprint;
import com.itheima.user.service.ItemsFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:itheima
 * @Description:ItemsFootprint业务层接口实现类
 *****/
@Service
public class ItemsFootprintServiceImpl implements ItemsFootprintService {

    @Autowired
    private ItemsFootprintMapper itemsFootprintMapper;


    /**
     * ItemsFootprint条件+分页查询
     * @param itemsFootprint 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<ItemsFootprint> findPage(ItemsFootprint itemsFootprint, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(itemsFootprint);
        //执行搜索
        return new PageInfo<ItemsFootprint>(itemsFootprintMapper.selectByExample(example));
    }

    /**
     * ItemsFootprint分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<ItemsFootprint> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<ItemsFootprint>(itemsFootprintMapper.selectAll());
    }

    /**
     * ItemsFootprint条件查询
     * @param itemsFootprint
     * @return
     */
    @Override
    public List<ItemsFootprint> findList(ItemsFootprint itemsFootprint){
        //构建查询条件
        Example example = createExample(itemsFootprint);
        //根据构建的条件查询数据
        return itemsFootprintMapper.selectByExample(example);
    }


    /**
     * ItemsFootprint构建查询对象
     * @param itemsFootprint
     * @return
     */
    public Example createExample(ItemsFootprint itemsFootprint){
        Example example=new Example(ItemsFootprint.class);
        Example.Criteria criteria = example.createCriteria();
        if(itemsFootprint!=null){
            // 
            if(!StringUtils.isEmpty(itemsFootprint.getId())){
                    criteria.andEqualTo("id",itemsFootprint.getId());
            }
            // 
            if(!StringUtils.isEmpty(itemsFootprint.getUsername())){
                    criteria.andLike("username","%"+itemsFootprint.getUsername()+"%");
            }
            // 
            if(!StringUtils.isEmpty(itemsFootprint.getSkuId())){
                    criteria.andEqualTo("skuId",itemsFootprint.getSkuId());
            }
            // 
            if(!StringUtils.isEmpty(itemsFootprint.getViewTime())){
                    criteria.andEqualTo("viewTime",itemsFootprint.getViewTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        itemsFootprintMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改ItemsFootprint
     * @param itemsFootprint
     */
    @Override
    public void update(ItemsFootprint itemsFootprint){
        itemsFootprintMapper.updateByPrimaryKey(itemsFootprint);
    }

    /**
     * 增加ItemsFootprint
     * @param itemsFootprint
     */
    @Override
    public void add(ItemsFootprint itemsFootprint){
        itemsFootprintMapper.insert(itemsFootprint);
    }

    /**
     * 根据ID查询ItemsFootprint
     * @param id
     * @return
     */
    @Override
    public ItemsFootprint findById(String id){
        return  itemsFootprintMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询ItemsFootprint全部数据
     * @return
     */
    @Override
    public List<ItemsFootprint> findAll() {
        return itemsFootprintMapper.selectAll();
    }
}
